package com.bojx.report.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class HttpClient {

	public static void main(String[] args) {
		String uri="index.htm";
		if (args.length!=0){
			uri=args[0];
		}
		doGet("localhost",8080,uri);
	}

	private static void doGet(String host, int port, String uri) {
		Socket socket=null;
		try {
			socket=new Socket(host,port);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		StringBuffer sb=new StringBuffer("GET "+uri+" HTTP/1.1\r\n");
		sb.append("Accept:*/*\r\n");
		sb.append("Accept-Language:zh-CN,zh;q=0.8\r\n");
		try {
			OutputStream os=socket.getOutputStream();
			os.write(sb.toString().getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//接受响应结果
		try {
			Thread.sleep(200);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			InputStream in=socket.getInputStream();
			int size=in.available();
			byte[] buffer=new byte[size];
			in.read(buffer);
			System.out.println(new String(buffer));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		try {
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
